var historyKindMap = new Map();
historyKindMap.set('-1','换电柜');
historyKindMap.set('0','1#充电位');
historyKindMap.set('1','2#充电位');
historyKindMap.set('2','3#充电位');
historyKindMap.set('3','4#充电位');
historyKindMap.set('4','5#充电位');
historyKindMap.set('5','6#充电位');
historyKindMap.set('6','7#充电位');
historyKindMap.set('7','8#充电位');
historyKindMap.set('8','9#充电位');
historyKindMap.set('9','10#充电位');
historyKindMap.set('10','11#充电位')


const typeState=new Map();
typeState.set("min","最小值")
typeState.set("max","最大值")
typeState.set("average","平均值")



  /*
  对象：换电柜测点概要信息
  1、key=测点ID；value=new Array(测点名称，数据类型，Is历史数据，显示类型，显示内容，单位)
     按照页面上的显示顺序，定义Map的项目顺序。

  2、显示类型： 0=直接显示，1=枚举显示

  3、显示内容：
  A、当DisplayType=1(枚举显示)时，按以下格式设置数据，分别以半角冒号和分号间隔。
    (前提条件：数据内容和显示文字中不包含半角冒号和分号)
    数据内容:显示内容;数据内容:显示内容;
    如："0:0(正常);1:(维护)"

  4、数据类型：
      0：字符串
      1：十进制数字
      2：十进制小数
      3：16进制数
   */
 var cabinetItemMap = new Map();

cabinetItemMap.set('0021',new Array('高温报警上限阀值',1,1,0,'','℃'));
cabinetItemMap.set('0022',new Array('低温报警下限阀值',1,1,0,'','℃'));
cabinetItemMap.set('0024',new Array('自动灭火功能启用',1,1,1,'0:0(禁用);1:1(启用)',''));
cabinetItemMap.set('0027',new Array('自动充电下的复充功能启用/禁用',1,1,1,'0:0(禁用);1:1(启用)',''));
cabinetItemMap.set('0043',new Array('换电soc阈值',1,1,0,'',''));
cabinetItemMap.set('0100',new Array('mqtt连接通道',0,0,0,'',''));
cabinetItemMap.set('0101',new Array('mqtt下模块数据上报间隔',0,1,0,'','秒'));
cabinetItemMap.set('0102',new Array('mqtt全量上报采样间隔',0,1,0,'','秒'));
cabinetItemMap.set('0103',new Array('mqtt模块心跳发送间隔',0,1,0,'','秒'));
cabinetItemMap.set('0110',new Array('自动控制功能启用',1,1,1,'0:0(禁用);1:1(启用)',''));
cabinetItemMap.set('0111',new Array('高温报警上线阈值回差',1,1,0,'','℃'));
cabinetItemMap.set('0112',new Array('低温报警下限阀值回差',1,1,0,'','℃'));
cabinetItemMap.set('0113',new Array('停止充电低温阈值',1,1,0,'','℃'));
cabinetItemMap.set('0114',new Array('开始充电低温回差',1,1,0,'','℃'));
cabinetItemMap.set('0115',new Array('停止充电高温阈值',1,1,0,'','℃'));
cabinetItemMap.set('0116',new Array('开始充电高温回差',1,1,0,'','℃'));
cabinetItemMap.set('0117',new Array('充满单体电压阈值',1,1,0,'','mV'));
cabinetItemMap.set('0118',new Array('复冲 SOC 掉电阈值',1,1,0,'',''));
cabinetItemMap.set('0120',new Array('加热开始电池包温度',1,1,0,'','℃'));
cabinetItemMap.set('0121',new Array('加热截止电池包 温度',1,1,0,'','℃'));
cabinetItemMap.set('0122',new Array('电池散热温度阈值',1,1,0,'','℃'));
cabinetItemMap.set('0123',new Array('电池散热关闭温度阈值',1,1,0,'','℃'));
cabinetItemMap.set('0130',new Array('消防动作一级电芯温度',1,1,0,'','℃'));
cabinetItemMap.set('0131',new Array('消防动作二级温度阈值',1,1,0,'','℃'));
cabinetItemMap.set('0132',new Array('消防动作判定时长（默认）',1,1,0,'','秒'));
cabinetItemMap.set('0140',new Array('加热器开启间隔',1,1,0,'','秒'));
cabinetItemMap.set('1001',new Array('整柜工作状态',1,1,1,'0:0(待机);1:1(换电验证);2:2(换电);3:3(维护验证);4:4(维护)',''));
cabinetItemMap.set('1002',new Array('配电位开门状态',1,1,1,'0:0(关);1:1(开)',''));
cabinetItemMap.set('1003',new Array('工控位开门状态',1,1,1,'0:0(关);1:1(开)',''));
cabinetItemMap.set('1004',new Array('气瓶1放空指示',1,1,1,'0:0(正常);1:1(放空)',''));
cabinetItemMap.set('1005',new Array('水浸1告警',1,1,1,'0:0(正常);1:1(报警)',''));
cabinetItemMap.set('1006',new Array('一次电源断开',1,1,1,'0:0(正常);1:1(断开)',''));
cabinetItemMap.set('11EF',new Array('消防位开门状态',1,0,1,'0:0(关);1:1(开)',''));

/*
对象：换电柜充电位测点信息(充电池测点信息:仅获取【Is电池=1】的项目)
1、key=测点ID；value=new Array(测点名称，数据类型，Is历史数据，显示类型，显示内容，单位，累进值，Is电池)
   按照页面上的显示顺序，定义Map的项目顺序。

2、显示类型：同cabinetItemMap

3、显示内容：同cabinetItemMap

4、累进值：充电位相关的累进值是1，电池相关的累进值是0x100

5、Is电池：1和0

6、实际上一共有11个充电位的测点信息，这里只需定义1#充电位的测点即可，2#以后充电位的Key根据累进值自动计算获取
 */
var chargePointItemMap = new Map();

chargePointItemMap.set('0010',new Array('充电位维护状态',1,1,1,'0:0(正常);1:1(维护)','',0x1,0));
chargePointItemMap.set('0030',new Array('充电位加热器启用',1,1,1,'0:0(禁用);1:1(启用)','',0x1,0));
chargePointItemMap.set('1010',new Array('充电位开门状态',1,1,1,'0:0(关门);1:1(开门)','',0x1,0));
chargePointItemMap.set('1020',new Array('充电器开关状态',1,1,1,'0:0(关);1:1(开)','',0x1,0));
chargePointItemMap.set('1030',new Array('充电位故障状态',1,1,1,'0:0(正常);1:1(故障)','',0x1,0));
chargePointItemMap.set('1040',new Array('充电位电池通讯状态',1,1,1,'0:0(异常);1:1(正常)','',0x1,0));
chargePointItemMap.set('1050',new Array('充电位电池短路检测状态',1,1,1,'0:0(开路);1:1(短路)','',0x1,0));
chargePointItemMap.set('1060',new Array('烟感报警状态',1,1,1,'0:0(正常);1:1(报警)','',0x1,0));
chargePointItemMap.set('1070',new Array('加热装置工作状态',1,1,1,'0:0(待机);1:1(工作)','',0x1,0));
chargePointItemMap.set('1080',new Array('起火报警状态',1,1,1,'0:0(正常);1:1(报警)','',0x1,0));
chargePointItemMap.set('1090',new Array('高温报警状态',1,1,1,'0:0(正常);1:1(报警)','',0x1,0));
chargePointItemMap.set('10A0',new Array('低温报警状态',1,1,1,'0:0(正常);1:1(报警)','',0x1,0));
chargePointItemMap.set('10B0',new Array('电池在位状态',1,1,1,'0:0(空位);1:1(在位)','',0x1,0));
chargePointItemMap.set('1520',new Array('充电位硬件故障状态',1,1,1,'0:0(正常);1:1(故障)','',0x1,0));
chargePointItemMap.set('2000',new Array('电池包-MOS温度',1,1,0,'','℃',0x100,1));
chargePointItemMap.set('2001',new Array('电池包-电池温度1',1,1,0,'','℃',0x100,1));
chargePointItemMap.set('2002',new Array('电池包-电池温度2',1,1,0,'','℃',0x100,1));
chargePointItemMap.set('2003',new Array('电池包-最高电池温度',1,1,0,'','℃',0x100,1));
chargePointItemMap.set('2004',new Array('电池包-最低电池温度',1,1,0,'','℃',0x100,1));
chargePointItemMap.set('2005',new Array('电池包-系统总电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2006',new Array('电池包-系统总电流',1,1,0,'','mA',0x100,1));
chargePointItemMap.set('2007',new Array('电池包-电池包编码',0,0,0,'','',0x100,1));
chargePointItemMap.set('2008',new Array('电池包-1#电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2009',new Array('电池包-2#电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200A',new Array('电池包-3#电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200B',new Array('电池包-4#电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200C',new Array('电池包-5#电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200D',new Array('电池包-6电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200E',new Array('电池包-7电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('200F',new Array('电池包-8电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2010',new Array('电池包-9电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2011',new Array('电池包-10池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2012',new Array('电池包-11电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2013',new Array('电池包-12电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2014',new Array('电池包-13电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2015',new Array('电池包-14电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2016',new Array('电池包-15电池电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2018',new Array('电池包-最大电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('2019',new Array('电池包-最小电压',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('201A',new Array('电池包-电压压差',1,1,0,'','mV',0x100,1));
chargePointItemMap.set('201B',new Array('电池包-充电过温保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('201C',new Array('电池包-短路保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('201D',new Array('电池包-放电过流保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('201E',new Array('电池包-充电过流保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('201F',new Array('电池包-总电压欠压保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2020',new Array('电池包-单节电池欠压保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2021',new Array('电池包-总电压过压保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2022',new Array('电池包-单节电池过压保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2023',new Array('电池包-前端采集错误',1,1,1,'0:0(正常);1:1(错误)','',0x100,1));
chargePointItemMap.set('2024',new Array('电池包-放电低温保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2025',new Array('电池包-充电低温保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2026',new Array('电池包-放电过温保护',1,1,1,'0:0(正常);1:1(保护)','',0x100,1));
chargePointItemMap.set('2027',new Array('电池包-电池充放电状态',1,1,1,'1:1(充电);2:2(放电);3:3(静置)','',0x100,1));
chargePointItemMap.set('2028',new Array('电池包-电池容量SOC',1,1,0,'','%',0x100,1));
chargePointItemMap.set('2029',new Array('电池包-剩余容量',1,1,0,'','mAH',0x100,1));
chargePointItemMap.set('202A',new Array('电池包-满充容量',1,1,0,'','mAH',0x100,1));
chargePointItemMap.set('202B',new Array('电池包-设计容量',1,0,0,'','mAH',0x100,1));
chargePointItemMap.set('202C',new Array('电池包-循环次数',1,1,0,'','次',0x100,1));
chargePointItemMap.set('202D',new Array('电池包-BMS编码',0,0,0,'','',0x100,1));
chargePointItemMap.set('202F',new Array('电池包-通讯定位模块编码',0,0,0,'','',0x100,1));
chargePointItemMap.set('2030',new Array('电池包-厂家名称',0,0,0,'','',0x100,1));
chargePointItemMap.set('2031',new Array('电池包-型号名称',0,0,0,'','',0x100,1));
chargePointItemMap.set('2032',new Array('电池包-硬件版本号',0,0,0,'','',0x100,1));
chargePointItemMap.set('2033',new Array('电池包-软件版本号',0,0,0,'','',0x100,1));


//数组索引定义
const index_of_name = 0;             //测点名称
const index_of_data_kind = 1;       //数据类型
const index_of_history = 2;         //Is历史数据
const index_of_dispaly_kind = 3;   //显示类型
const index_of_dispaly_data = 4;   //显示内容
const index_of_symbol = 5;          //单位
const index_of_add = 6;             //累进值
const index_of_bms = 7;             //Is电池
const key_of_bms_is_inline = '10B0' //电池电池在位状态Key


/**
 * 获取监测点名称
 * @param map
 * @param key
 * @returns {*}
 */
function getNameOfMoniterItem(map, key) {
  try {
    var arr = map.get(key);
    return arr[index_of_name];
  } catch (err) {
    return '';
  }
}

/**
 * 获取可显示历史数据项目的Map
 * 获取
 *
 * @param map
 * @param index:获取换电柜基础数据时，传入-1；获取1#充电位数据时，传入0；2#到11#依次传入1,2,3...10
 *
 * @returns {Map<any, any>}
 */
function getMapOfHistoryItem(map, index) {
  var historyMap = new Map();
  var key,keyes;
  var keyes=[...map.keys()]
  for (var i=0; i<keyes.length;i++ ) {
    key = keyes[i];
    var arr = map.get(key);
    if (arr[index_of_history] === 1) {
      //换算对应充电位项目的地址
      if (index > 0) {
        key = getKeyOfNextItem(map, key, index);
      }
      historyMap.set(key, arr[index_of_name]);
    }
  }
  return historyMap;
}

/**
 * 获取指定充电位同一类型监测点的地址(Key)，返回16进制格式的字符串，如：0011,001A
 *
 * @param map
 * @param key
 * @param index
 */
function getKeyOfNextItem(map, key, index) {

  var newKey;
  var Keyprogressive = '';
  var arr = map.get(key);
  var  progressive=arr[index_of_add];
  var newKey=(parseInt(key,"16") + progressive * index).toString(16);

  for(var i=0; i<(4-newKey.length); i++) {
    Keyprogressive = '0' + Keyprogressive;
  }
  newKey = Keyprogressive + newKey;

  return newKey;
}

/**
 * 获取电池包历史数据map
 * @param map
 * @returns {Map<any, any>}
 */
function getBmsMap(map) {
  var bmsMap=new Map()
  var key, keyMap
  var keyMap = [...map.keys()]
  for (var i = 0; i < keyMap.length; i++) {
    key = keyMap[i]
    var arr=map.get(key)
    if (arr[index_of_history]==1&&arr[index_of_bms]==1){
      key=key.substr(key.length-2,key.length)
      bmsMap.set(key,arr[index_of_name])
    }
  }
  return bmsMap;
}

/**
 * 返回历史数据曲线显示用的真实数据类型的值
 * 从数据库得到的值都是字符串形式，需要转换成数字
 *
 * @param map
 * @param key
 * @param data
 */
function getRealData(map, key, data) {

}

/**
 * 返回实时数据显示页面上的显示内容。
 * 根据显示类型，如果是枚举类型，结合具体数据值，返回对应的显示内容。
 * 如果有单位值，连同单位一起返回
 *
 * @param map
 * @param key
 * @param data
 */
function getDisplayData(map, key, data) {
  var arr = map.get(key);
  if(arr[index_of_dispaly_kind]==0){
    return data + arr[index_of_symbol]
  }else if(arr[index_of_dispaly_kind]==1) {
    var str = arr[index_of_dispaly_data].split(";")
    var valueMap = new Map()
    for(var index in str){
      var str1 = str[index].split(":")
      valueMap.set(str1[0], str1[1])
    }

    var result = data
    if(valueMap.has(data)){
      result = valueMap.get(data)

    }
    return result + arr[index_of_symbol]
  }
}


export {getMapOfHistoryItem,getNameOfMoniterItem,getDisplayData,getBmsMap,getKeyOfNextItem,index_of_bms,chargePointItemMap,historyKindMap,cabinetItemMap,key_of_bms_is_inline,typeState}
